package com.zj.core.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.fileutils.FontUtils
import com.zj.core.R
import java.util.ArrayList
import com.zj.core.ui.ItemListDetailUI
import org.jetbrains.anko.AnkoContext
import org.jetbrains.anko.find

/**
 * Created by Administrator on 2019/5/30.
 */
class DetailListAdapter(private val mContext: Context, private val mDetailList: ArrayList<String>) :
    RecyclerView.Adapter<DetailListAdapter.RecordOcrAdapterHolder>(){
    private var mTextColor: Int = 0

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecordOcrAdapterHolder {
        return RecordOcrAdapterHolder(ItemListDetailUI().createView(AnkoContext.create(parent.context,parent)))
    }

    override fun onBindViewHolder(holder: RecordOcrAdapterHolder, position: Int) {
        with(holder){
            if (mTextColor != 0) {
                this.typeNameTv.setTextColor(mTextColor)
                this.typeValueTv.setTextColor(mTextColor)
            }

            val valueSplit = mDetailList[position].split("\\|".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            if (valueSplit.isNotEmpty()) {
                this.typeNameTv.text = valueSplit[0]
                if (valueSplit.size > 1) {
                    this.typeValueTv.text = valueSplit[1]

                    this.typeValueTv.post {
                        if (FontUtils.meauseWidthForText(mContext, valueSplit[1]) > this.typeValueTv.width) {
                            this.pushImgView.visibility = View.VISIBLE
                        } else {
                            this.pushImgView.visibility = View.INVISIBLE
                        }
                    }
                }
            }

            this.pushImgView.setOnClickListener{
                detailShowView(this, position)
            }

            this.parentView.setOnClickListener{ //Item展开折叠
                detailShowView(this, position)
            }
        }
    }

    /**
     * 展开折叠
     *
     * @param holder
     * @param position
     */
    private fun detailShowView(holder: RecordOcrAdapterHolder, position: Int) {
        val mSplit = mDetailList[position].split("\\|".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        var mStrBuf = ""
        if (mSplit.size > 1) {
            mStrBuf = mSplit[1]
        }

        with(holder){
            if (this.pushImgView.tag == null || this.pushImgView.tag == "0") {
                this.pushImgView.setBackgroundResource(R.mipmap.simple_img)
                this.pushImgView.tag = "1"

                this.typeValueTv.maxLines = 99
                this.typeValueTv.text = mStrBuf
            } else {                                                         //折叠
                this.pushImgView.setBackgroundResource(R.mipmap.detail_img)
                this.pushImgView.tag = "0"

                this.typeValueTv.maxLines = 1
                this.typeValueTv.text = mStrBuf
            }
        }
    }

    override fun getItemCount(): Int {
        return mDetailList.size
    }

    fun setTextColor(textColor: Int) {
        this.mTextColor = textColor
    }

    inner class RecordOcrAdapterHolder(view: View) : RecyclerView.ViewHolder(view) {
        var typeNameTv: TextView=view.find(R.id.type_name_tv)
        var typeValueTv: TextView= view.find(R.id.type_value_tv)
        var parentView: LinearLayout =view.find(R.id.parent_lly)
        var pushImgView: ImageView=view.find(R.id.push_img_view)
        var recordItemLine: View = itemView.find(R.id.record_item_line)
    }
}